﻿Imports System.Data.OleDb

Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'The if not ispostback = if I got to this page NOT from this page, then load saved data
        If Not IsPostBack Then

            Dim prPhotoPath As String = ""
            Dim ctryID As String = ""

            'Load current data from the Profile table
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * from PROFILE WHERE USER_UNAME=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()

            'Since we need to get back a record - establish data reader
            Dim dr As OleDbDataReader = cmd.ExecuteReader
            If dr.HasRows Then
                dr.Read()
                'fill in the textboxes and profile with the data returned (as long as there is something - not null)
                If Not IsDBNull(dr("USER_FNAME")) Then tb_PrFName.Text = dr("USER_FNAME")
                profile_FirstName.Text = dr("USER_FNAME").ToString

                If Not IsDBNull(dr("USER_LNAME")) Then tb_PrLName.Text = dr("USER_LNAME")
                profile_LastName.Text = dr("USER_LNAME").ToString

                If Not IsDBNull(dr("CTRY_ID")) Then ddl_PrCountry.SelectedValue = dr("CTRY_ID")
                ctryID = dr("CTRY_ID").ToString

                If Not IsDBNull(dr("USER_DOB")) Then tb_PrDOB.Text = dr("USER_DOB")
                profile_DOB.Text = tb_PrDOB.Text = dr("USER_DOB").ToString

                If Not IsDBNull(dr("USER_EMAIL")) Then tb_PrEmail.Text = dr("USER_EMAIL")
                profile_Email.Text = dr("USER_EMAIL").ToString

                If Not IsDBNull(dr("USER_BIO")) Then tb_PrBio.Text = dr("USER_BIO")
                profile_Biography.Text = dr("USER_BIO").ToString

                If Not IsDBNull(dr("USER_PHOTO")) Then prPhotoPath = "Image/" & dr("USER_PHOTO")
                profile_Image.ImageUrl = dr("USER_PHOTO").ToString
            End If

            'Load current data from the Country table
            Dim cOleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
            Dim cSqlString As String = "SELECT CTRY_NAME from COUNTRY WHERE CTRY_ID=@f1"
            Dim cCmd As OleDbCommand = New OleDbCommand(cSqlString, cOleDbConn)
            cCmd.CommandType = CommandType.Text
            cCmd.Parameters.AddWithValue("@f1", ctryID)
            cOleDbConn.Open()
            Dim cDr As OleDbDataReader = cCmd.ExecuteReader
            If cDr.HasRows Then
                cDr.Read()
                If Not IsDBNull(cDr("CTRY_NAME")) Then profile_Country.Text = cDr("CTRY_NAME")
            End If

        End If
    End Sub

    Protected Sub btn_PrSaveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_PrSaveProfile.Click

        'Variable declaration separated from assignment because of if statements
        Dim newFileName As String = ""
        Dim SqlString As String

        'Checking if user has uploaded a new profile image
        If fup_PrPhoto.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + ".jpg"

            If fup_PrPhoto.HasFile Then
                fup_PrPhoto.SaveAs(Request.MapPath("Image") & "/" & newFileName)
            End If
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)

        'Depending on whether the user has uploaded a new profile image
        If fup_PrPhoto.HasFile Then
            'Option 1 - The user has uploaded a new profile image
            SqlString = "UPDATE PROFILE SET USER_FNAME=@f1,USER_LNAME=@f2,CTRY_ID=@f3,USER_DOB=@f4,USER_EMAIL=@f5,USER_BIO=@f6,USER_PHOTO=@f7 WHERE USER_UNAME=@f8"
        Else
            'Option 2 - The user has NOT uploaded a new profile image
            SqlString = "UPDATE PROFILE SET USER_FNAME=@f1,USER_LNAME=@f2,CTRY_ID=@f3,USER_DOB=@f4,USER_EMAIL=@f5,USER_BIO=@f6 WHERE USER_UNAME=@f8"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_PrFName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_PrLName.Text)
        cmd.Parameters.AddWithValue("@f3", ddl_PrCountry.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", tb_PrDOB.Text)
        cmd.Parameters.AddWithValue("@f5", tb_PrEmail.Text)
        cmd.Parameters.AddWithValue("@f6", tb_PrBio.Text)

        'If the user has uploaded a profile image
        If fup_PrPhoto.HasFile Then
            cmd.Parameters.AddWithValue("@f7", "Image/" & newFileName)
        End If

        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'Clear everything from memory and close the db connection
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        'Response.Redirect("Profile.aspx?User=" & User.Identity.Name)
        Response.Redirect("EditProfile.aspx")
    End Sub

End Class